找传奇、传世资源到传世资源站!

用android蓝牙与RTK通讯源码

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

android手机上先通过蓝牙链接上RTK,打开程序,打开蓝牙,接收数据,显示数据。主要应用与解析RTK中的数据。
from clipboardpackage com.lan;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.UnsupportedEncodingException;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;import android.widget.ToggleButton;import android.widget.RadioGroup.OnCheckedChangeListener;public class RelayControl extends Activity implements OnCheckedChangeListener{public static boolean isRecording = false;// 线程控制标记private ToggleButton tgbt1,tgbt2,tgbt3,tgbt4;private Button releaseCtrl,btBack,btSend;private OutputStream outStream = null;private EditText _txtRead,_txtSend ;private ConnectedThread manageThread;private Handler mHandler;private RadioGroup radioType;private RadioButton rbPC;private String encodeType ="GBK";@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.relaycontrol);//接收线程启动manageThread=new ConnectedThread();mHandler = new MyHandler();manageThread.Start();findMyView();setMyViewListener(); setTitle("返回前需先关闭socket连接");//接收区不可见_txtRead.setCursorVisible(false); //设置输入框中的光标不可见_txtRead.setFocusable(false); //无焦点}private void findMyView() {tgbt1=(ToggleButton) findViewById(R.id.toggleButton1);tgbt2=(ToggleButton) findViewById(R.id.toggleButton2);tgbt3=(ToggleButton) findViewById(R.id.toggleButton3);tgbt4=(ToggleButton) findViewById(R.id.toggleButton4);releaseCtrl=(Button)findViewById(R.id.button1);btBack=(Button) findViewById(R.id.button2);btSend=(Button) findViewById(R.id.btSend); radioType = (RadioGroup) findViewById(R.id.radio_type); rbPC =(RadioButton) findViewById(R.id.rb_pc); _txtRead = (EditText) findViewById(R.id.etShow);_txtSend = (EditText) findViewById(R.id.etSend);}private void setMyViewListener() {// 监听RadioButtonradioType.setOnCheckedChangeListener(this);rbPC.setChecked(true);//设置为默认选中为 PC通信tgbt1.setOnClickListener(new ClickEvent());tgbt2.setOnClickListener(new ClickEvent());tgbt3.setOnClickListener(new ClickEvent());tgbt4.setOnClickListener(new ClickEvent());releaseCtrl.setOnClickListener(new ClickEvent());btBack.setOnClickListener(new ClickEvent());btSend.setOnClickListener(new ClickEvent());}public void onDestroy() { try {lylist.btSocket.close();} catch (IOException e) {e.printStackTrace();} super.onDestroy(); }public void onCheckedChanged(RadioGroup group, int checkedId){switch(checkedId){case R.id.rb_pc:encodeType="GBK";break;case R.id.rb_phone:encodeType="UTF-8";break;default:break;}}privateclass ClickEvent implements View.OnClickListener {public void onClick(View v) {if (v == releaseCtrl)// 释放连接{try {lylist.btSocket.close();manageThread.Stop();//testBlueTooth.serverThread.cancel();//Toast.makeText(getApplicationContext(), "socket连接已关闭", Toast.LENGTH_SHORT);setTitle("socket连接已关闭");} catch (IOException e) {//Log .e(TAG,"ON RESUME: Unable to close socket during connection failure", e2);//Toast.makeText(getApplicationContext(), "关闭连接失败", Toast.LENGTH_SHORT);setTitle("关闭连接失败");}} else if (v == btBack) {// 返回RelayControl.this.finish();} else if (v == tgbt1)// 继电器按钮1{if (tgbt1.isChecked() == false)sendMessage("11");else if (tgbt1.isChecked() == true)sendMessage("10");} else if (v == tgbt2) {if (tgbt2.isChecked() == false)sendMessage("21");else if (tgbt2.isChecked() == true)sendMessage("20");} else if (v == tgbt3) {if (tgbt3.isChecked() == false)sendMessage("31");else if (tgbt3.isChecked() == true)sendMessage("30");} else if (v == tgbt4) {if (tgbt4.isChecked() == false)sendMessage("41");else if (tgbt4.isChecked() == true)sendMessage("40");}else if (v == btSend){String infoSend =_txtSend.getText().toString();sendMessage(infoSend);setTitle("发送成功");}}} public static void setEditTextEnable(TextView view,Boolean able){ // view.setTextColor(R.color.read_only_color); //设置只读时的文字颜色 if (view instanceof android.widget.EditText){ view.setCursorVisible(able); //设置输入框中的光标不可见 view.setFocusable(able); //无焦点 view.setFocusableInTouchMode(able); //触摸时也得不到焦点 } }public void sendMessage(String message) {//控制模块try {outStream = lylist.btSocket.getOutputStream();} catch (IOException e) {// TODO Auto-generated catch block//Log.e(TAG, "ON RESUME: Output stream creation failed.", e);Toast.makeText(getApplicationContext(), " Output stream creation failed.", Toast.LENGTH_SHORT);}byte[] msgBuffer = null;try {msgBuffer = message.getBytes(encodeType);//编码} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();Log.e("write", "Exception during write encoding GBK ", e1);}//while(true){try {outStream.write(msgBuffer);//Toast.makeText(getApplicationContext(), "发送数据中..", Toast.LENGTH_SHORT);setTitle("成功发送指令:" message);} catch (IOException e) {// TODO Auto-generated catch block//Log.e(TAG, "ON RESUME: Exception during write.", e);Toast.makeText(getApplicationContext(), "发送数据失败", Toast.LENGTH_SHORT);}} class ConnectedThread extends Thread {private InputStream inStream = null;// 蓝牙数据输入流private long wait;private Thread thread;public ConnectedThread() {isRecording = false;this.wait=50;thread =new Thread(new ReadRunnable());}public void Stop() {isRecording = false;//thread.stop();//State bb = thread.getState();}public void Start() {isRecording = true;State aa = thread.getState();if(aa==State.NEW){thread.start();}else thread.resume();}private class ReadRunnable implements Runnable {public void run() {while (isRecording) {try {inStream = lylist.btSocket.getInputStream();} catch (IOException e) {// TODO Auto-generated catch block//Log.e(TAG, "ON RESUME: Output stream creation failed.", e);Toast.makeText(getApplicationContext(), " input stream creation failed.", Toast.LENGTH_SHORT);}//char[]dd= new char[40]; int length=20;byte[] temp = new byte[length];//String readStr="";//keep listening to InputStream while connectedif (inStream!= null) {try{int len = inStream.read(temp,0,length-1);Log.e("available", String.valueOf(len));//setTitle("available" len);if (len > 0) {byte[] btBuf = new byte[len];System.arraycopy(temp, 0, btBuf, 0, btBuf.length);// for(int iii=0;iii<len;iii ){ // String out = new Character ((char) btBuf[iii]).toString(); // readStr = readStr out; //// } // InputStreamReader inputStreamReader = new InputStreamReader(inStream, "UTF-8"); // int bb = inputStreamReader.read(dd,0,dd.length-1); // String TempStr=new String(String.valueOf(dd).trim().getBytes()); //读编码 String readStr1 = new String(btBuf,encodeType); mHandler.obtainMessage(01,len,-1,readStr1).sendToTarget(); } Thread.sleep(wait);// 延时一定时间缓冲数据}catch (Exception e) {// TODO Auto-generated catch blockmHandler.sendEmptyMessage(00);}}}}}} private class MyHandler extends Handler{ @Override public void dispatchMessage(Message msg) { switch(msg.what){ case 00: isRecording=false; _txtRead.setText(""); _txtRead.setHint("socket连接已关闭"); //_txtRead.setText("inStream establishment Failed!"); break; case 01: String info=(String) msg.obj; _txtRead.append(info); break; default: break; } } }}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复